package com.bstek.sample.controller;

import com.bstek.bdf2.core.redis.BstekRedisCache;
import com.bstek.bdf2.core.redis.InfoBody;
import com.bstek.sample.domain.TbUser;
import com.bstek.sample.domain.User;
import com.bstek.sample.mapper.ITbUserMapper;
import com.bstek.sample.mapper.IUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Title: UserController
 * @Package: com.bstek.sample.controller
 * @Description:
 * @Author: zxq
 * @Date: 2024/1/3 - 20:14
 */

@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
public class TbUserController {


    @Autowired
    private ITbUserMapper userMapper;

    @Resource
    private BstekRedisCache bstekRedisCache;



    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/test")
    @ApiOperation("获取用户")
    public Object loadUsers(){
        TbUser user = userMapper.selectById(1L);
        return user;
    }



    @GetMapping("/redis")
    @ApiOperation("获取缓存用户")
    public Object loadUserByCache(){
        InfoBody infoBody = new InfoBody<Long,TbUser>();
        infoBody.setId(1L);
        infoBody.setType(TbUser.class);
        infoBody.setKeyPrefix("tb:user:");
        infoBody.setFallback(this::selectById);
        Object o = bstekRedisCache.queryRedisOrSave(infoBody);
        return o;

    }

    private Object selectById(Object o) {
        return selectById(Long.valueOf(o.toString()));
    }


    private Object selectById(Long id){
        TbUser user = userMapper.selectById(1L);
        return user;
    }

}
